
using System.Windows.Controls;
using NUnit.Framework;
using Rhino.Mocks;
using ShadeTree.Binding.Settings;
using ShadeTree.Binding.WPF;

namespace ShadeTree.Binding.Tests.WPF
{
    [TestFixture]
    public class PicklistElementTester
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
            LastChosenValues.Clear();
        }

        #endregion

        [Test]
        public void BindThenResetThenApplyChanges()
        {
            var box = new ComboBox();

            foreach (var item in new[] { "TX", "CT", "MO" })
            {
                box.Items.Add(item);
            }

            Accessor property = SingleProperty.Build<PicklistTarget>(x => x.State);
            var target = new PicklistTarget();

            var element = new PicklistElement(property, box);
            target.State = "CT";

            element.Bind(target);
            Assert.AreEqual(target.State, box.SelectedItem);

            box.SelectedItem = "TX";
            element.Reset();
            Assert.AreEqual(target.State, box.SelectedItem);

            box.SelectedItem = "TX";
            element.ApplyChanges();
            Assert.AreEqual("TX", target.State);
        }


        [Test]
        public void GetACallback()
        {
            Accessor property = SingleProperty.Build<PicklistTarget>(x => x.State);
            var target = new PicklistTarget();
            var box = new ComboBox();

            foreach (var item in new[] { "a", "b", "c" })
            {
                box.Items.Add(item);
            }

            var element = new PicklistElement(property, box);
            element.Bind(target);

            bool wasCalled = false;
            element.RegisterChangeHandler(delegate { wasCalled = true; });
            box.SelectedItem = "b";

            Assert.IsTrue(wasCalled);
        }

        [Test, Ignore("can't get the event to fire")]
        public void LostFocusTellsTheParentScreenBinderToValidate()
        {
            var box = new ComboBox();
            foreach (var item in new[] { "TX", "CT", "MO" })
            {
                box.Items.Add(item);
            }

            Accessor property = SingleProperty.Build<PicklistTarget>(x => x.State);
            var target = new PicklistTarget();

            var element = new PicklistElement(property, box);

            var mocks = new MockRepository();
            using (mocks.Record())
            {
                var binder = mocks.StrictMock<IScreenBinder>();
                element.Binder = binder;

                binder.Validate(element);
            }

            using (mocks.Playback())
            {
                TestUtility.FireEvent(box, "LostFocus");
            }
        }

        [Test]
        public void SetDefaultsWhenThereIsOnlyOneChoiceInTheList()
        {
            Accessor property = SingleProperty.Build<PicklistTarget>(x => x.State);
            var target = new PicklistTarget();
            var box = new ComboBox();
            box.Items.Add("c");
            var element = new PicklistElement(property, box);
            element.Bind(target);

            target.State.ShouldBeNull();
            element.SetDefaults();
            target.State.ShouldEqual("c");
        }


        [Test]
        public void SetDefaultsWhenThereIsTwoChoiceInTheListDoesNothing()
        {
            Accessor property = SingleProperty.Build<PicklistTarget>(x => x.State);
            var target = new PicklistTarget();
            var box = new ComboBox();
            foreach (var item in new[] { "b", "c" })
            {
                box.Items.Add(item);
            }

            var element = new PicklistElement(property, box);
            element.Bind(target);

            target.State.ShouldBeNull();
            element.SetDefaults();
            target.State.ShouldBeNull();
        }

        [Test]
        public void SettingADefaultFiresChangeEvents()
        {
            Accessor property = SingleProperty.Build<PicklistTarget>(x => x.State);
            var target = new PicklistTarget();
            var box = new ComboBox();
            box.Items.Add("c");
            var element = new PicklistElement(property, box);
            element.Bind(target);


            bool iWasCalled = false;
            element.RegisterChangeHandler(( ) => iWasCalled = true);
            element.SetDefaults();
            iWasCalled.ShouldBeTrue();
        }
    }

    internal class PicklistTarget
    {
        public string State { get; set; }


        public string[] Colors { get; set; }
    }
}